import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:ytbusiness/config/url.dart';
import 'package:ytbusiness/models/scout_login_model.dart';
import 'package:ytbusiness/repository/cook.dart';
import 'package:ytbusiness/repository/scout_request.dart';
import 'package:ytbusiness/state/base_model.dart';
import 'package:ytbusiness/utils/helper.dart';

///勘察员登录
class ScoutLoginViewModel extends BaseModel {
  TextEditingController email = TextEditingController(); //登录邮箱
  TextEditingController unit = TextEditingController(); //单位
  TextEditingController passWord = TextEditingController(); //登录密码
  ScoutRequest _scoutRequest;

  ///
  ScoutLoginViewModel(ScoutRequest scoutRequest) {
    this._scoutRequest = scoutRequest;
  }

  ///
  login(BuildContext context, Map<String, dynamic> data) async {
    print("data$data");
    doLoading(true);
    Map<String, dynamic> map = await _scoutRequest.scoutLogin(
        context: context, data: data) as Map<String, dynamic>;
    doLoading(false);
    ScoutLoginModel scoutLoginModel = ScoutLoginModel.fromJson(map);
    if (scoutLoginModel.code == 1001) {
      if (scoutLoginModel.code.toString() == "1001") {
        ///清空历史cookie
        // (await Cook.cookieJar).deleteAll();
        ///登录成功保存cookies

        List<Cookie> cookies =
            (await Cook.cookieJar).loadForRequest(Uri.parse(Url.scoutLogin));
        (await Cook.cookieJar)
            .saveFromResponse(Uri.parse(Url.scoutLogin), cookies);

        urlPush(context, "yuntu://scout");
      } else {
        toast("1wewe");
        toast(scoutLoginModel.message.toString());

        //urlPush(context, "yuntu://scout");
      }
    } else {
      toast("wewe");
      toast(scoutLoginModel.message);

      urlPush(context, "yuntu://scout");
    }
  }
}
